#include "../include/list.h"
	
/******************list_t begin******************/
bool list_is_empty(const list_t * head) {
	if (head->next == head)
		return true;
	return false;	
}

inline bool list_is_exist(const list_t * head, const list_t * entry) {
        list_t * p;
        if (list_is_empty(head)) {
                return false;
        }
        p = head->next;
        while (p != NULL) {
                if (p == entry) {
                        return true;
                }
                p = p->next;
        }
        return false;
}
/******************
两种可能,list 为空，list 不为空
******************/
inline void list_add_head(list_t * head, list_t * new){
#ifdef SAFE_TYPE
	if (list_is_exist(head, new)) {
		return;
	}
#endif
	if (list_is_empty(head)) {
		new->next = NULL;
		head->next = new;
	}
	else {
		new->next = head->next;
		head->next = new;
	}
}

inline void list_add_tail(list_t * head, list_t * new){
	list_t * p;
#ifdef SAFE_TYPE
	if (list_is_exist(head, new)) {
		return;
	}	
#endif
	p = head->next;
	for ( ;p != head && p->next != NULL; p = p->next);
	new->next = NULL;
	p->next = new;		
}

inline void list_insert(list_t * entry, list_t * new){

	new->next = entry->next;
	entry->next = new;
}

inline void list_del(list_t * head, list_t * entry) {
	list_t * p = head->next;
	list_t * q = head;
	
	/** list is empty , noting can be deleted **/
	if ( list_is_empty(head) ) {
		return ;
	}
	while (p){
		if (p == entry){
			q->next = p->next;
			break;	
		}
		q = p;	
		p = p->next;
	}
}

inline void list_empty(list_t * head) {
	head->next = head;	
}
/********************list end********************/

/********************c_clist begin****************/

inline bool c_list_is_empty(const c_list_t * head) {
	if (head->next == head)
                return true;
        return false;							
}

inline bool c_list_is_exist(const c_list_t * head,const c_list_t * entry) {
        c_list_t * p;
	p = head->next;
        while (p != head) {
                if (p == entry) {
                        return true;
                }
                p = p->next;
        }
        return false;
}

inline void c_list_add_head(c_list_t * head, c_list_t * new){

#ifdef SAFE_TYPE
	if (c_list_is_exist(head, new)) {
		return;
	}
#endif
	if (c_list_is_empty(head)) {
                new->next = head; 
                head->next = new;
        }
        else {
                new->next = head->next;
                head->next = new;
        }	
}


inline void c_list_add_tail(c_list_t * head, c_list_t * new){
	c_list_t * p;
#ifdef SAFE_TYPE
	if (c_list_is_exist(head, new)) {
		return;
	}
#endif
	p = head->next;
        for ( ;p != head && p->next != head; p = p->next);
        new->next = head;
        p->next = new;	
}


inline void c_list_insert(c_list_t * entry, c_list_t * new){
	new->next = entry->next;
        entry->next = new;	
}

inline void c_list_del(c_list_t * head, c_list_t * entry){
	c_list_t * p = head->next;
        c_list_t * q = head;

        /** list is empty , noting can be deleted **/
        if ( c_list_is_empty(head) ) {
                return ;
        }
        while (p != head){
                if (p == entry){
                        q->next = p->next;
                        break;
                }
                q = p;
                p = p->next;
        }
}

inline void c_list_empty(c_list_t * head) {
	head->next = head;	
}
/****************c_list end************************/

/****************dlist begin **********************/

inline bool dlist_is_empty(const dlist_t * head){
	if (head->next == head && head->prev == head) {
		return true;
	}	
	return false;
}

inline bool dlist_is_exist(const dlist_t * head, const dlist_t * entry) {
        dlist_t * p;
	p = head->next;
        while (p != head) {
                if (p == entry) {
                        return true;
                }
                p = p->next;
        }
        return false;
}

inline void dlist_add_head(dlist_t * head, dlist_t * new) {

#ifdef SAFE_TYPE
	if (dlist_is_exist(head, new)) {
		return;
	}	
#endif 
	new->next = head->next;
	new->prev = head;
	head->next->prev = new;
	head->next = new;
}

inline void dlist_add_tail(dlist_t * head, dlist_t * new) {

#ifdef SAFE_TYPE
	if (dlist_is_exist(head, new)) {
		return;
	}
#endif
	new->next = head;
	new->prev = head->prev;
	head->prev->next = new;	
	head->prev = new;
}

inline void dlist_insert(dlist_t * entry, dlist_t * new) {

	new->next = entry->next;
	new->prev = entry;
	entry->next->prev = new;
	entry->next = new;	
}

inline void dlist_del(dlist_t * head, dlist_t * entry) {
	if (dlist_is_empty(head)) {
		return ;
	}
	entry->prev->next = entry->next;
	entry->next->prev = entry->prev;	
}

inline void dlist_empty(dlist_t * head) {
	head->next = head;
	head->prev = head; 
}

/*****************dlist end ************************/
